package com.yiku.utils;


import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;

import com.taobao.api.ApiException;
import com.yiku.common.DingConstant;
import com.yiku.common.URLConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 获取access_token工具类
 */
public class AccessTokenUtil {
    private static final Logger bizLogger = LoggerFactory.getLogger(AccessTokenUtil.class);

    public static String getToken() throws RuntimeException {
        try {



            DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_INNO_TOKKEN);
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.setAppkey(DingConstant.APPKEY);
            request.setAppsecret(DingConstant.APPSECRET);
            request.setHttpMethod("GET");
            OapiGettokenResponse response = client.execute(request);
            String accessToken = response.getAccessToken();
            return accessToken;
        } catch (ApiException e) {
            bizLogger.error("getAccessToken failed", e);
            throw new RuntimeException();
        }


    }
    public static String getThirdToken() throws RuntimeException {
        try {
            DefaultDingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_GET_TOKKEN);
            OapiServiceGetCorpTokenRequest req= new OapiServiceGetCorpTokenRequest();
            req.setAuthCorpid(DingConstant.CORP_ID);
            OapiServiceGetCorpTokenResponse execute= client.execute(req,DingConstant.CustomKey,DingConstant.CustomSecret,"suiteTicket");

            String accessToken = execute.getAccessToken();

            System.out.println(accessToken);
            return accessToken;
        } catch (ApiException e) {
            bizLogger.error("getAccessToken failed", e);
            throw new RuntimeException();
        }


    }
}
